<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Admin_IndexController
 *
 * @author hoquangminh
 */
class Admin_IndexController extends Zend_Controller_Action {
    
    public function init() {
        parent::init();
        
        $this->_helper->layout()->setLayout("layout");
        $this->view->translate = Zend_Registry::get("translate");
    }
    
    public function indexAction() {
        $config_form = new Admin_Form_Config();
        $this->view->form = $config_form;
        $page_db = new Core_Model_DbTable_Pages();
        if ($this->getRequest()->isPost()) {
            $form_data = $this->getRequest()->getParams();
            if ($config_form->isValid($form_data)) {
                $id_social_page = $config_form->getValue("id_social_page");
                $data = array (
                    'description'       => $config_form->getValue("description"),
                    'post_point'        => $config_form->getValue("post_point"),
                    'comment_point'     => $config_form->getValue("comment_point"),
                    'like_point'        => $config_form->getValue("like_point"),
                    'vote_point'        => $config_form->getValue("vote_point"),
                    'background_color'  => $config_form->getValue("background_color"),
                    'update_frequency'  => $config_form->getValue("update_frequency"),
                    'reset_period'      => $config_form->getValue("reset_period")
                );
                
                $banner_images = $config_form->banner_image->receive();
                if (!empty($banner_images)) {
                    $data['banner_image'] = $banner_images[0];
                } elseif (isset($form_data['hdd_banner_image'])) {
                    $data['banner_image'] = $form_data['hdd_banner_image'];
                }
                
                $fangate_images = $config_form->fangate_image->receive();
                if (!empty($fangate_images)) {
                    $data['fangate_image'] = $fangate_images[0];
                } elseif (isset($form_data['hdd_fangate_image'])) {
                    $data['fangate_image'] = $form_data['hdd_fangate_image'];
                }
                
                $page_db->update($data, "id_social_page = '$id_social_page'");
                $this->view->page_info = $data;
            } else {
                $page_info = $this->getRequest()->getPost();
                $id_social_page = $page_info['id_social_page'];
                $this->view->page_info = $page_info;
                $form_messages = $config_form->getMessages();
                $errors = array();
                foreach ($form_messages as $field => $messages) {
                    foreach ($messages as $messages) {
                        $errors[$field][] = $messages;
                    }
                }
                $this->view->errors = $errors;
            }
        } else {
            $id_social_page = $this->_getParam("pid");
            $page_info = $page_db->getPageInfo($id_social_page)->toArray();
            $this->view->page_info = $page_info;
        }
        
        $this->view->id_social_page = $id_social_page;
    }
}

?>
